package com.xframe.domain.views.builder;

import cn.hutool.json.JSONUtil;
import com.xframe.domain.BaseModel;
import com.xframe.domain.emnus.Constant;
import com.xframe.domain.emnus.XDelFlag;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.sys.SysModelAttr;
import com.xframe.domain.sys.SysModelConfig;
import com.xframe.domain.views.IModelConfigBuilder;
import com.xframe.xdal.core.constant.MappingConstants;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.serialize.JsonModel;
import com.xframe.xdal.core.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 创建默认的模型配置(default)
 * @ClassName: ModelConfigBuilder
 * @Author: xufeng
 * @CreateTime: 2023/12/06
 **/
@Log4j2
public class ModelConfigBuilder implements IModelConfigBuilder {

    static final String CONFIG_TYPE = "ser";
    static final String CN_LANGUAGE = "cn";
    static final String EN_LANGUAGE = "en";

    static final String DATE_PROCCLASS = "com.xframe.xdal.core.serialize.proc.DataTimeSerProc";
    static final String DATE_PROCCLASS_ARGS = "yyyy-MM-dd HH:mm:ss";
    static final String REF = "ref";

    @Override
    public List<SysModelConfig> builder(SysModel sysModel, IUserCredentials userCredentials) {
        List<SysModelConfig> sysModelConfigs = new ArrayList<>();
        SysModelConfig modelCnConfig = SysModelConfigCreate(sysModel,userCredentials,CN_LANGUAGE);
        SysModelConfig modelEnConfig = SysModelConfigCreate(sysModel,userCredentials,EN_LANGUAGE);

        String json = setSysModelConfigContent(sysModel,userCredentials);
        modelCnConfig.setContent(json);
        modelEnConfig.setContent(json);


        sysModelConfigs.add(modelCnConfig);
        sysModelConfigs.add(modelEnConfig);
        return sysModelConfigs;
    }

    public String setSysModelConfigContent(SysModel sysModel, IUserCredentials userCredentials) {
        List<SysModelAttr> sysModelAttrList = sysModel.getModelAttrList();

        List<JsonModel> jsonModelList = new ArrayList<>();
        for(SysModelAttr modelAttr : sysModelAttrList){
            JsonModel jsonModel = new JsonModel();

            jsonModel.setName(modelAttr.getAttrName());
            jsonModel.setMapName(modelAttr.getAttrName());
            jsonModel.setRefModelCode(StringUtil.EMPTY);
            jsonModel.setFlag(StringUtil.EMPTY);
            jsonModel.setProcClass(StringUtil.EMPTY);
            jsonModel.setClassArgs(StringUtil.EMPTY);
            jsonModel.setHeader(modelAttr.getDesc());

            String attrType = modelAttr.getType();

            if(attrType.equals(java.util.Date.class.getName())){
                jsonModel.setProcClass(DATE_PROCCLASS);
                jsonModel.setClassArgs(DATE_PROCCLASS_ARGS);
            }
            jsonModelList.add(jsonModel);
        }
        List<IRelation> relationList = sysModel.getBelongsToMappingList();
        for(IRelation relation : relationList){
            JsonModel jsonModel = new JsonModel();
            jsonModel.setName(relation.getAs());
            jsonModel.setMapName(relation.getAs());
            jsonModel.setRefModelCode(relation.getDataType().getName());
            jsonModel.setRefModelType(MappingConstants.MAPPING_REF_OBJ);
            jsonModel.setFlag(REF);
            jsonModel.setProcClass(StringUtil.EMPTY);
            jsonModel.setClassArgs(StringUtil.EMPTY);
            jsonModelList.add(jsonModel);
        }
        return JSONUtil.toJsonStr(jsonModelList);

    }

    protected SysModelConfig SysModelConfigCreate(SysModel sysModel, IUserCredentials userCredentials,String language){
        SysModelConfig modelConfig = new SysModelConfig();
        modelConfig.setConfigType(CONFIG_TYPE);
        modelConfig.setModelCode(sysModel.getModelCode());
        modelConfig.setSystemFlag(Constant.DEF_SYSTEMFLAG);
        modelConfig.setLanguage(language);
        modelConfig.setQueryKey(Constant.DEF_QUERY_KEY);
        modelConfig.setVer(Constant.DEF_VER);
        modelConfig.setBaseAttrVal(userCredentials);
        modelConfig.setDelFlag(XDelFlag.getDefFlag());

        return modelConfig;
    }
}
